package com.summer.campussct.controller;


import com.summer.campussct.annotation.Permission;
import com.summer.campussct.pojo.User;
import com.summer.campussct.pojo.dto.RestResult;
import com.summer.campussct.pojo.dto.UserQueryParam;
import com.summer.campussct.service.IUserService;
import com.summer.campussct.utils.UserIdHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户商户信息表，敏感信息可能会加密存储，因此设置为256个字符 前端控制器
 * </p>
 *
 * @author Mr.Chen
 * @since 2024-04-21
 */
@Api(tags = "用户控制器")
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService userService;

    @ApiOperation("登录接口{account/phone, password}")
    @PostMapping("login")
    public RestResult login(@RequestBody Map<String, String> loginForm){
        return userService.login(loginForm);
    }

    @ApiOperation("注册接口")
    @PostMapping("register")
    public RestResult register(@RequestBody Map<String, String> registerForm){
        return userService.register(registerForm);
    }

    @ApiOperation("获取登录用户基本信息")
    @GetMapping("userInfo")
    @Permission
    public RestResult getUserInfo(){
        long userId = UserIdHolder.getId();
        return userService.getUserInfo(userId);
    }

    @ApiOperation("获取用户基本信息")
    @GetMapping("userDetails/{id}")
    @Permission
    public RestResult getUserDetails(@PathVariable("id") long id){
        return userService.getUserDetails(id);
    }

    @ApiOperation("修改登录用户的基本信息")
    @PutMapping("modify")
    @Permission
    public RestResult modify(@RequestBody User user){
        long userId = UserIdHolder.getId();
        user.setId(userId);
        return userService.modify(user);
    }

    @ApiOperation("编辑用户")
    @PutMapping("modifyUser")
    @Permission
    public RestResult modifyUser(@RequestBody User user){
        return userService.modifyUser(user);
    }

    @ApiOperation("分页条件查询用户列表")
    @PostMapping("listCondition")
    @Permission
    public RestResult getListCondition(@RequestBody UserQueryParam userQueryParam){
        return userService.getListCondition(userQueryParam);
    }

    @ApiOperation("获取用户角色id")
    @GetMapping("roleId/{id}")
    @Permission
    public RestResult getRoleIdByUser(@PathVariable("id") long id){
        return userService.getRoleIdByUser(id);
    }

    @ApiOperation("给用户分配角色")
    @PutMapping("assignRole")
    @Permission
    public RestResult assignRole(@RequestBody User user){
        return userService.assignRole(user);
    }

    @ApiOperation("禁用用户")
    @PutMapping("disableUser/{id}")
    @Permission
    public RestResult deleteRole(@PathVariable("id") long id,
                                 @RequestParam Integer status){
        return userService.disableUser(id, status);
    }

    @ApiOperation("添加用户")
    @PostMapping("add")
    @Permission
    public RestResult addUser(@RequestBody @Validated User user){
        return userService.addUser(user);
    }

    @ApiOperation("修改头像")
    @PostMapping(value = "modifyAvatar", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @Permission
    public RestResult modifyAvatar(@RequestPart("file") MultipartFile file){
        return userService.modifyAvatar(file);
    }

    @ApiOperation("获取用户的权限及范围")
    @GetMapping("getPermRange")
    @Permission
    public RestResult getPermRange(){
        long uid = UserIdHolder.getId();
        return userService.getPermRange(uid);
    }
}

